package cn.chengpengper.seckill.order.application.service.impl;

import cn.chengpengper.dubbo.interfaces.goods.SeckillGoodsDubboService;
import cn.chengpengper.seckill.common.code.HttpCode;
import cn.chengpengper.seckill.common.exception.SeckillException;
import cn.chengpengper.seckill.common.model.dto.SeckillGoodsDTO;
import cn.chengpengper.seckill.order.application.model.command.SeckillOrderCommand;
import cn.chengpengper.seckill.order.application.place.SeckillPlaceOrderService;
import cn.chengpengper.seckill.order.application.security.SecurityService;
import cn.chengpengper.seckill.order.application.service.SeckillSubmitOrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * @author chengpeng.hu
 */
public abstract class SeckillBaseSubmitOrderServiceImpl implements SeckillSubmitOrderService {

    @Autowired
    private SecurityService securityService;

    @DubboReference(version = "1.0.0", check = false)
    private SeckillGoodsDubboService seckillGoodsDubboService;

    @Autowired
    protected SeckillPlaceOrderService seckillPlaceOrderService;

    @Override
    public void checkSeckillOrder(Long userId, SeckillOrderCommand seckillOrderCommand) {
        if (Objects.isNull(userId) || Objects.isNull(seckillOrderCommand)) {
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        if (!securityService.securityPolicy(userId)) {
            throw new SeckillException(HttpCode.USER_INVALID);
        }
        SeckillGoodsDTO seckillGoods = seckillGoodsDubboService.getSeckillGoods(seckillOrderCommand.getGoodsId(), seckillOrderCommand.getVersion());
        seckillPlaceOrderService.checkSeckillOrder(seckillOrderCommand, seckillGoods);
    }

}
